export const host = 'https://api.xinzhulicai.com/v1'
export const imgHost ='http://img.xinzhulicai.com'
export const mapKey ='4MCBZ-RD763-THS3X-YXH37-FGZC5-OYBN6'
export const mapSig ='dpifZIebqxqZfFmeEcwM97eDNGZH6Mu'

export let jwtKey = ''

exports.getJwt = function () {
  return jwtKey;
}
exports.setJwt = function (jwt) {
  jwtKey = jwt
}
exports.fileContainDel = function (array, objID) {
  if (array && objID) {
    let flag = false
    let delIndex = -1
    for (var i = 0; i < array.length; i++) {
      if (array[i].id === objID) {
        flag = true
        delIndex = i
        break
      }
    }
    if (flag) {
      array.splice(delIndex, 1)
    }
    return array
  }
}
exports.fileConcat = function (array, obj) {
  if (array && obj) {
    let flag = true
    for (let file of array) {
      if (file.id === obj.id) {
        flag = false
        break
      }
    }
    if (flag) {
      array.push(obj)
    }
    return array
  }
}
exports.cloneObj = (obj) => {
  var newObj = {};
  if (obj instanceof Array) {
    newObj = []
  }
  for (var key in obj) {
    var val = obj[key]
    newObj[key] = typeof val === 'object' ? exports.cloneObj(val) : val
  }
  return newObj
}
exports.wxRunAsync = (execute) => {
  return new Promise((resolve, reject) => {
    execute(resolve, reject)
  })
}
exports.wxPost = (url, paramData, successCallback, failCallback, completeCallback) => {
  wx.request({
    url: host + url,
    method: 'POST',
    data: paramData,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization': jwtKey
    },
    success(res) {
      if (successCallback) {
        if (typeof successCallback === 'function') {
          successCallback(res)
        }
      }
      if (res.data) {
        getApp().globalData.serverBusy = res.data.errorCode === 100
      }
    },
    fail(res) {
      if (failCallback) {
        if (typeof failCallback === 'function') {
          failCallback(res)
        }
      }
    },
    complete(res) {
      if (completeCallback) {
        if (typeof completeCallback === 'function') {
          completeCallback(res)
        }
      }
    }
  })
}

exports.wxGet = (url, paramData, successCallback, failCallback, completeCallback) => {
  wx.request({
    url: host + url,
    data: paramData,
    header: {
      'Authorization': jwtKey
    },
    success(res) {
      if (successCallback) {
        if (typeof successCallback === 'function') {
          successCallback(res)
        }
      }
      if (res.data) {
        getApp().globalData.serverBusy = res.data.errorCode === 100
      }
    },
    fail(res) {
      if (failCallback) {
        if (typeof failCallback === 'function') {
          failCallback(res)
        }
      }
    },
    complete(res) {
      if (completeCallback) {
        if (typeof completeCallback === 'function') {
          completeCallback(res)
        }
      }
    }
  })
}
exports.formatTime = function (hour, minute) {
  let timeStr = ''
  if (hour < 10) {
    timeStr += '0'
  }
  timeStr += hour
  timeStr += ':'
  if (minute < 10) {
    timeStr += '0'
  }
  timeStr += minute
  return timeStr
}
exports.timeToHM = function (time) {
  let times=time.split(':')
  let h = parseInt(times[0], 10) 
  let m = parseInt(times[1], 10) 
  return {h,m}
}
exports.isTel=function(tel){
  return /^1[345789]\d{9}$/.test(tel)
}

exports.formatDate=function(date,hmsFlag) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if (hmsFlag){
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }else{
    return [year, month, day].map(formatNumber).join('-')
  }
}
exports.compareDate=function(d1,d2){
  let d1Time = new Date(d1).getTime()
  let d2Time = new Date(d2).getTime()
  return d1Time > d2Time
}


exports.Rpx2PX = function (rpx) {
  return rpx / 750 * wx.getSystemInfoSync().windowWidth
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

exports.commonShare=function(title,path){
  return {
    title: title||'快来加入和我一起挣佣金吧',
    //imageUrl: imgUrl,
    path: path||'pages/index/index'
  }
}

exports.showToast = function (msg, durtion, icon) {
  wx.showToast({
    title: msg || '',
    icon: 'none' || icon,
    duration: durtion || 1500
  })
}
exports.serviceBusy = function () {
  wx.showToast({
    title: '服务器维护中,请稍后访问!',
    icon: 'none',
    duration: 3000
  })
}
exports.numFormat = function (num) {
  if (isNaN(num) || !num) return num
  num = parseFloat(num)
  let sNum = num.toFixed(2)    //将传入的值【四舍五入】保留2位小数
  sNum = num.toLocaleString()
  return sNum
}